package DBH;

import DBH.DBHelper;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.Vector;
import javax.swing.JOptionPane;

public class DBHelper {

    private static Connection conn=null ;
    public DBHelper() {
        if (conn == null) {
        }
            try {
                String conStr = "jdbc:sqlserver://localhost;databaseName=do_an";
                 File nick = new File("nick.txt");
                File pass = new File("pass.txt");
                FileInputStream is = new FileInputStream(nick);
                FileInputStream pa = new FileInputStream(pass);
                Scanner input = new Scanner(is, "UTF-8");//đọc theo bảng mã utf-8
                String line = input.nextLine();
                String userName = line;
                Scanner input1 = new Scanner(pa, "UTF-8");//đọc theo bảng mã utf-8
                String line1 = input1.nextLine();
                String Password = line1;
                is.close();
                input.close();
                pa.close();
                input1.close();
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                conn = DriverManager.getConnection(conStr, userName, Password);
            } catch (ClassNotFoundException ex) {
            } catch (SQLException ex) {
            } catch (IOException e) {
                //e.printStackTrace();
            }        
        }
    public static ResultSet executeQuery(String spName) throws SQLException {
        if (conn != null) {
            CallableStatement cs = conn.prepareCall("{call " + spName + "}");
            return cs.executeQuery();
            
        }
        return null;
    }
    public static ResultSet executeQuery(String spName, Vector paramList) throws SQLException {
        if (conn != null) {
            String strQ = "{call " + spName + "(";
            int t = 0;
            for (Object obj : paramList) {
                if (t != 0) {
                    strQ += ",";
                }
                if (obj instanceof Integer) {
                    Integer i = (Integer) obj;
                    strQ += i.toString();
                } else if (obj instanceof String) {
                    String s = (String) obj;
                    strQ += "'" + s + "'";
                }
                t++;
            }
            strQ += ")}";

            CallableStatement cst = conn.prepareCall(strQ);
            return cst.executeQuery();
        }
        return null;
    }
    public static int executeUpdate(String spName, Vector paramList) throws SQLException {
        if (conn != null) {
            String strQ = "{call " + spName + "(";

            int t = 0;
            for (Object obj : paramList) {
                if (t != 0) {
                    strQ += ",";
                }
                if (obj instanceof Integer) {
                    Integer i = (Integer) obj;
                    strQ += i.toString();
                } else if (obj instanceof Float) {
                    Float f = (Float) obj;
                    strQ += f.toString();
                } else if (obj instanceof String) {
                    String s = (String) obj;
                    strQ += "'" + s + "'";
                }
                t++;
            }
            strQ += ")}";

            CallableStatement cst = conn.prepareCall(strQ);
            return cst.executeUpdate();
        }
        return -1;
    }

    public static void main(String[] a) {
        new DBHelper();
    }
}
